<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="员工姓名" prop="employeeName">
      <el-input v-model="dataForm.employeeName" placeholder="员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="employeeIdnum">
      <el-input v-model="dataForm.employeeIdnum" placeholder="员工身份证号"></el-input>
    </el-form-item>
      <el-form-item label="性别" prop="employeeSex">
        <el-select v-model="dataForm.employeeSex" placeholder="请选择">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="联系电话" prop="employeePhone">
      <el-input v-model="dataForm.employeePhone" placeholder="联系电话"></el-input>
    </el-form-item>
      <el-form-item label="所属部门" prop="departId">
        <el-select v-model="dataForm.departId" placeholder="请选择" @change="getPostByDepart(dataForm.departId)">
          <el-option v-for="(item,i) in departModel" :label="item.departName" :value="item.departId"></el-option>
        </el-select>
      </el-form-item>
<!--    <el-form-item label="所属部门" prop="employeeDepart">-->
<!--      <el-input v-model="dataForm.employeeDepart" placeholder="所属部门"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="所属岗位" prop="postId">
        <el-select v-model="dataForm.postId" placeholder="请选择">
          <el-option v-for="(item,i) in postModel" :label="item.postName" :value="item.postId"></el-option>
        </el-select>
      </el-form-item>
<!--    <el-form-item label="所属岗位" prop="employeePost">-->
<!--      <el-input v-model="dataForm.employeePost" placeholder="所属岗位"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="入职日期" prop="employeeTime">
        <el-date-picker
          v-model="dataForm.employeeTime"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="员工属性" prop="employeeAttribute">
        <el-select v-model="dataForm.employeeAttribute" placeholder="请选择">
          <el-option label="在编" value="在编"></el-option>
          <el-option label="合同工" value="合同工"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false,clearPost()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    created () {
      this.getDepart()
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          employeeId: '',
          employeeName: '',
          employeeIdnum: '',
          employeeSex: '',
          employeePhone: '',
          employeeDepart: '',
          employeePost: '',
          employeeTime: '',
          employeeAttribute: '',
          createTime: '',
          updateTime: '',
          departId: '',
          postId: ''
        },
        dataRule: {
          employeeId: [
            { required: true, message: '员工编号不能为空', trigger: 'blur' }
          ],
          employeeName: [
            { required: true, message: '员工姓名不能为空', trigger: 'blur' }
          ],
          employeeIdnum: [
            { required: true, message: '员工身份证号不能为空', trigger: 'blur' }
          ],
          employeeSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          employeePhone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          employeeDepart: [
            { required: true, message: '所属部门不能为空', trigger: 'blur' }
          ],
          employeePost: [
            { required: true, message: '所属岗位不能为空', trigger: 'blur' }
          ],
          employeeTime: [
            { required: true, message: '入职日期不能为空', trigger: 'blur' }
          ],
          employeeAttribute: [
            { required: true, message: '员工属性 在编 合同工不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        },
        departModel: {
          departId: '',
          departName: ''
        },
        postModel: {
          postId: '',
          postName: '1'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_employee/bsemployee/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.employeeId = data.bsEmployee.employeeId
                this.dataForm.employeeName = data.bsEmployee.employeeName
                this.dataForm.employeeIdnum = data.bsEmployee.employeeIdnum
                this.dataForm.employeeSex = data.bsEmployee.employeeSex
                this.dataForm.employeePhone = data.bsEmployee.employeePhone
                this.dataForm.employeeDepart = data.bsEmployee.employeeDepart
                this.dataForm.employeePost = data.bsEmployee.employeePost
                this.dataForm.employeeTime = data.bsEmployee.employeeTime
                this.dataForm.employeeAttribute = data.bsEmployee.employeeAttribute
                this.dataForm.createTime = data.bsEmployee.createTime
                this.dataForm.updateTime = data.bsEmployee.updateTime
                this.dataForm.departId = parseInt(data.bsEmployee.employeeDepart)
                this.dataForm.postId = parseInt(data.bsEmployee.employeePost)
                console.log(666)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_employee/bsemployee/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'employeeId': this.dataForm.employeeId,
                'employeeName': this.dataForm.employeeName,
                'employeeIdnum': this.dataForm.employeeIdnum,
                'employeeSex': this.dataForm.employeeSex,
                'employeePhone': this.dataForm.employeePhone,
                'employeeDepart': this.dataForm.departId,
                'employeePost': this.dataForm.postId,
                'employeeTime': this.dataForm.employeeTime,
                'employeeAttribute': this.dataForm.employeeAttribute,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              this.postModel = ''
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getDepart () {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_post/bspost/getDepart`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.departModel = data.date
          }
        })
      },
      getPostByDepart (departId) {
        this.postModel = ''
        this.dataForm.postId = ''
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_employee/bsemployee/getPostByDepart?departId=` + departId),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.postModel = data.date
          }
        })
      },
      clearPost () {
        this.postModel = ''
      },
      getPostById (postId) {
        this.$http({
          url: this.$http.adornUrl(`/tb_bs_employee/bsemployee/getPostById?postId=` + postId),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.postModel.postName = data.date.postName
            this.postModel.postId = parseInt(data.date.postId)
          }
        })
      }
    }
  }
</script>
